package it.ictdays12.cipcip;

import java.util.Comparator;

import android.text.format.Time;

public class TagCounter {
	
	// Hashtag.
	String hashtag;
	
	// Time elapsed (in millisecond).
	long timeElapsed;
	
	// 'Popularity' counter.
	int counter;
	
	// 'Popularity' percent.
	double percent;
	
	public double round(double number, int digits){
	    return Math.round( number * Math.pow( 10, digits ) )/Math.pow( 10, digits );
	}
	
	public long getTimeElapsed() {
		return timeElapsed;
	}
	
	public void setTimeElapsed(long time) {
		timeElapsed = time;
	}
	
	public double getPercent() {
		return percent;
	}
	
	public void setPercent(double perc) {
		percent = perc;
		percent = round(percent, 2);
	}
	
	public String getTag() {
		return hashtag;
	}
	
	public int getCounter() {
		return counter;
	}
	
	public TagCounter(String tag) {
		hashtag 	= tag;
		counter 	= 1;
		timeElapsed = 0;
		percent 	= 0;
	}
	
	public TagCounter(String tag, int count, long time) {
		hashtag 	= tag;
		counter 	= count;
		timeElapsed = time;
		percent 	= 0;
	}
	
	public TagCounter(String tag, long time) {
		hashtag 	= tag;
		counter 	= 1;
		timeElapsed = time;
		percent 	= 0;
	}
	
	public void incrementCounter() {
		++counter;
	}

	@Override
	public boolean equals(Object o) {
		TagCounter tc = (TagCounter) o;
		long timeDiff = (Math.abs(tc.timeElapsed - this.timeElapsed));
		if (tc.hashtag.equals(this.hashtag) && timeDiff < 3600000) {
			return true;
		}
		return false;
	}

	public String formatHour(Long hour) {
		long timeAgo 	= System.currentTimeMillis() - timeElapsed;
		long hours 		= timeAgo / 3600000;
		
		if (hours == 0)
		{
			long minutes = timeAgo / 60000;
			return String.valueOf(minutes) + " min. ago";
		}
		
		return String.valueOf(hours) + " hours ago";
	}
	
	@Override
	public String toString() {
		return "TAG: " + hashtag + ", " + formatHour(timeElapsed) +  ".";
	}
}